<template>
  <div class="list-sort">
    <button
      type="button"
      class="list-sort__btn"
      :class="{ 'is-active': isOpen }"
      @click="isOpen = !isOpen"
    >
      {{ activeName }}
    </button>
    <ul
      class="list-sort__list"
      :class="{ 'is-active': isOpen }"
    >
      <li v-for="item in list" :key="item.key || item">
        <button
          type="button"
          class="list-sort__option"
          :class="{ 'is-active': activeKey == (item.key || item) }"
          @click="updateKey(item.key || item)"
        >
          {{ item.name || item }}
        </button>
      </li>
    </ul>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'

// 定义组件名称
defineOptions({
  name: 'ListSort'
})

// 定义 props
const props = defineProps<{
  list?: any[]
  focusKey?: string | number | null
  year?: boolean
}>()

// 定义 emits
const emit = defineEmits<{
  updateKey: [key: string | number]
}>()

// 响应式数据
const isOpen = ref(false)

// 计算属性
const activeKey = computed(() => {
  if (props.year) {
    return props.focusKey ?? props.list?.[0]
  }
  return props.focusKey ?? props.list?.[0]?.key
})

const activeName = computed(() => {
  if (props.year) {
    const obj = props.list?.find((item) => item == activeKey.value)
    return obj ? obj : props.list?.[0]
  }
  const obj = props.list?.find((item) => item.key == activeKey.value)
  return obj ? obj.name : props.list?.[0]?.name
})

// 监听器
watch(() => props.focusKey, () => {
  closeHandler()
})

// 方法
const updateKey = (key: string | number) => {
  emit('updateKey', key)
  isOpen.value = false
}

const closeHandler = () => {
  isOpen.value = false
}
</script>

<style lang="scss" scoped>
.list-sort {
  position: relative;
}

.list-sort__btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  font-size: 14px;
  line-height: 18px;
  letter-spacing: .01em;
  color: #1a1c20;
  background: none;
  border: none;
  background: #fff;
  border-radius: 4px;
}

.list-sort__btn:after {
  content: "";
  width: 16px;
  height: 16px;
  background: url() 50% no-repeat;
  background-size: 100% auto;
  transform: rotate(90deg);
  transition: transform .3s ease-in-out;
}

.list-sort__btn.is-active:after {
  transform: rotate(270deg);
}

.list-sort__list {
  position: absolute;
  width: 100%;
  left: 0;
  top: calc(100% + 11px);
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 8px 32px 0 rgba(130,130,140,.24);
  overflow: hidden;
  transform: translateY(-20px);
  opacity: 0;
  pointer-events: none;
  transition: transform .3s ease-in-out,opacity .3s ease-in-out;
}

@media(max-width: 767.98px) {
  .list-sort__list {
    top:calc(100% + 8px);
  }
}

.list-sort__list.is-active {
  transform: translateY(0);
  opacity: 1;
  pointer-events: visible;
}

@media(max-width: 767.98px) {
  .list-sort__list {
    top:calc(100% + 8px);
  }
}

.list-sort__option {
  cursor: pointer;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.428;
  color: #1a1c20;
  background: none;
  border: none;
  background: #fff;
  transition: color .3s ease-in-out;
}

@media(max-width: 1023.98px) {
  .list-sort__option {
    line-height:1.285;
    letter-spacing: .03em;
    font-weight: 400;
  }
}

@media(max-width: 767.98px) {
  .list-sort__option {
    font-size:13px;
    line-height: 1.33;
    letter-spacing: .02em;
    font-size: 16px;
    font-weight: 400;
    line-height: 1.75;
  }
}

@media(max-width: 767.98px)and (max-width:1023.98px) {
  .list-sort__option {
    font-size:14px;
    line-height: 1.714;
    letter-spacing: .01em;
  }
}

.list-sort__option:hover {
  background: #f7f7fa;
}

.list-sort__option:active {
  background: #fff;
}

.list-sort__option:after {
  content: "";
  width: 16px;
  height: 16px;
  background: url() 50% no-repeat;
  background-size: 100% auto;
  opacity: 0;
  transition: opacity .3s ease-in-out;
}

.list-sort__option:not(:list-child) {
  border-top: 1px solid #f7f7fa;
}

.list-sort__option.is-active {
  color: #f04;
}

.list-sort__option.is-active:after {
  opacity: 1;
}
</style>
